package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.dao.impl.CompanyImpl;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.service.ICompanyService;
import redis.clients.jedis.Jedis;

public class CompanyServiceImpl implements ICompanyService {
    @Override
    public CompanyModel getCompanyModel() {
        /**
         * 数据来源与缓存
         * 第一阶段  数据  来源   页面
         * 第二阶段                model
         * 第三阶段               记事本
         * 第四阶段               MySQL
         * 第五阶段                redis
         * 服务逻辑层处理
         */
        /**
         * 当有数据修改时，我们需要重写对redis缓存数据进行修改
         * 1.设置我们的过期时间
         * 2.手动进行删除数据
         */
        final CompanyModel[] companyModel = {null};
        for (int i = 0; i < 100; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    companyModel[0] = getCompanyModelThead();
                }
            }).start();
        }
        return companyModel[0];
    }

    private synchronized CompanyModel getCompanyModelThead() {
        //首先查看缓存里面是否有数据
//        1.有   直接读取缓存
//        2.没有  查询数据库
        CompanyDao companyDao = new CompanyImpl();
        CompanyModel companyModel = new CompanyModel();
        Jedis jedis = new Jedis();


        String id = jedis.get("company_id");
        if (id != null) {
            companyModel.setLogo(jedis.get("company_logo"));
            companyModel.setPhone(jedis.get("company_phone"));
            companyModel.setAd(jedis.get("company_ad"));
            companyModel.setNet(jedis.get("company_net"));
            companyModel.setEmail(jedis.get("company_email"));
            companyModel.setLinePhone(jedis.get("company_line_phone"));
            companyModel.setAddress(jedis.get("company_address"));
            companyModel.setVersion(jedis.get("company_version"));
            System.out.println("缓存");
        } else {
            companyModel = companyDao.getCompanyModel();
            jedis.set("company_id", companyModel.getId().toString());
            jedis.expire("company_id", 30 * 60);
            jedis.set("company_logo", companyModel.getLogo());
            jedis.set("company_phone", companyModel.getPhone());
            jedis.set("company_ad", companyModel.getAd());
            jedis.set("company_net", companyModel.getNet());
            jedis.set("company_email", companyModel.getEmail());
            jedis.set("company_line_phone", companyModel.getLinePhone());
            jedis.set("company_address", companyModel.getAddress());
            jedis.set("company_version", companyModel.getVersion());
            System.out.println("数据库");
        }
        return companyModel;
    }

    @Override
    public int update(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyImpl();
        int count = companyDao.update(companyModel);
        if (count == 1) {
            Jedis jedis = new Jedis();
//            jedis.flushDB();//清除当前数据库数据
//            jedis.flushAll();//清除所有数据库数据
            jedis.del("company_id");
        }
        return count;
    }
}
